﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;
using MySql.Data.MySqlClient;
using Dapper;

namespace DAL
{
    public class AlertMakeRecordDAL
    {
        public static async Task<IEnumerable<AlertRecord>> GetAlertMakeRecordsAsync(string userId = null)
        {
            string sql = @"SELECT R.id
                            , R.realDataId
                            ,R.countDataId
                            ,R.insertTime
                            ,R.resolved
                            ,A.id AS AlertId
                            ,A.name AS AlertName
                            ,A.addMan
                            ,A.minVal
                            ,A.maxVal
                            ,S.stationId
                            ,S.stationType
                            ,S.stationCode
                            ,S.stationName
                            ,S.elementList
                            ,E.elementCode
                            ,E.elementName
                            FROM msap_db.alert_make_record R
                            INNER JOIN msap_db.alert_make_infor A
                            ON R.make_inforId = A.id
                            INNER JOIN msap_db.station_infor S
                            ON A.stationId = S.stationId
                            INNER JOIN msap_db.element_infor E
                            ON S.stationType = E.stationType
                            AND E.elementCode = A.elementCode
                            AND (@UserId IS NULL 
	                        OR EXISTS(
		                        SELECT 1 FROM msap_db.station_user SU 
                                WHERE 
                                SU.stationId = S.stationId 
                                AND SU.userId = @UserId))
                            ORDER BY R.resolved ASC";
            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return await conn.QueryAsync<AlertRecord, dynamic, AlertMakeInfo, StationInfo, ElementInfo, AlertRecord>(sql, (alertRecord, alertRenameParam, alertMakeInfo, stationInfo, elementInfo) =>
                {
                    alertMakeInfo.Id = alertRenameParam.AlertId;
                    alertMakeInfo.Name = alertRenameParam.AlertName;
                    alertMakeInfo.StationInfo = stationInfo;
                    alertMakeInfo.ElementInfo = elementInfo;
                    alertRecord.AlertMakeInfo = alertMakeInfo;
                    return alertRecord;
                },
                new { UserId = userId},
                splitOn: "AlertId,addMan,stationId,elementCode");
            }
        }

        public static Task<int> ResolvedAlertRecord(List<string> ids)
        {
            string sql = $@"UPDATE msap_db.alert_make_record
                            SET resolved = 1
                            WHERE id IN({string.Join(",", ids.Select(id => "'" + id + "'"))})";

            using (MySqlConnection conn = new MySqlConnection(AppSetting.Instance.MySqlConnectionStr))
            {
                return conn.ExecuteAsync(sql);
            }
        }
    }
}
